package cn.stylefeng.guns.modular.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.factory.TreeBuildFactory;
import cn.stylefeng.guns.modular.entity.Room;
import cn.stylefeng.guns.modular.mapper.RoomsMapper;
import cn.stylefeng.guns.modular.model.MiniAppTreeNode;
import cn.stylefeng.guns.modular.myenum.RoomStatusEnum;
import cn.stylefeng.guns.modular.service.RoomsService;
import cn.stylefeng.guns.modular.vo.RoomDetailVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author 李宝嘉
 * @Date 2021/12/23 14:27
 */
@Slf4j
@Service
public class RoomsServiceImpl extends ServiceImpl<RoomsMapper, Room> implements RoomsService {

    @Override
    public RoomDetailVO getRoomDetail(String roomId) {
        Room room = this.getById(roomId);
        RoomDetailVO roomDetailVO = new RoomDetailVO();
        BeanUtils.copyProperties(room, roomDetailVO);
        if (room.getPid() != 0L) {
            Room parentRoom = this.lambdaQuery().select(Room::getName).eq(Room::getId, room.getPid()).one();
            roomDetailVO.setParentName(parentRoom.getName());
        } else {
            roomDetailVO.setParentName("顶级");
        }
        return roomDetailVO;
    }

    @Override
    public void add(Room room) {
        this.save(room);
    }

    @Override
    public List<Room> list() {
        LambdaQueryWrapper<Room> roomQueryWrapper = new LambdaQueryWrapper<>();
        roomQueryWrapper.eq(Room::getStatus, RoomStatusEnum.OPEN.getCode());
        return this.list(roomQueryWrapper);
    }

    @Override
    public List<MiniAppTreeNode> tree() {
        List<MiniAppTreeNode> resultList = CollectionUtil.newArrayList();
        this.list().forEach(r -> {
            MiniAppTreeNode miniAppTreeNode = new MiniAppTreeNode();
            miniAppTreeNode.setId(r.getId());
            miniAppTreeNode.setPid(r.getPid());
            miniAppTreeNode.setLabel(r.getName());
            miniAppTreeNode.setValue(r.getId().toString());
            miniAppTreeNode.setSeatNums(r.getSeatNums());
            handleOpenTime(miniAppTreeNode, r);
            resultList.add(miniAppTreeNode);
        });
        List<MiniAppTreeNode> trees = new TreeBuildFactory<MiniAppTreeNode>().doTreeBuild(resultList);
        trees.forEach(tree -> {
            String pLabel = tree.getLabel();
            tree.getChildren().forEach(c -> {
                c.setRoomName(pLabel + "-" + c.getLabel());
            });
        });
        return trees;
    }

    @Override
    public List<MiniAppTreeNode> getTopLevel() {
        List<MiniAppTreeNode> resultList = CollectionUtil.newArrayList();
        MiniAppTreeNode top = new MiniAppTreeNode();
        top.setValue("0");
        top.setLabel("顶级");
        top.setChildren(Collections.EMPTY_LIST);
        resultList.add(top);
        this.lambdaQuery().eq(Room::getPid, 0L).list().forEach(r -> {
            MiniAppTreeNode miniAppTreeNode = new MiniAppTreeNode();
            miniAppTreeNode.setLabel(r.getName());
            miniAppTreeNode.setValue(r.getId().toString());
            miniAppTreeNode.setChildren(Collections.EMPTY_LIST);
            resultList.add(miniAppTreeNode);
        });
        return resultList;
    }

    @Override
    public Date getCloseTimeBySeatId(Long seatId) {
        return this.baseMapper.getCloseTimeBySeatId(seatId);
    }

    private void handleOpenTime(MiniAppTreeNode miniAppTreeNode, Room r) {
        if (ObjectUtil.isNull(r.getOpenDate())) return;
        StringBuffer sBuffer = new StringBuffer();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        sBuffer.append(sdf.format(r.getOpenDate())).append("-");
        sBuffer.append(sdf.format(r.getCloseDate()));
        miniAppTreeNode.setOpenTime(sBuffer.toString());
    }
}
